﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using MyDMS.DAL;

public partial class DepartmentPage : PageBaseClass
{
    MyDMSEntity _myDmsEntity = new MyDMSEntity();
    protected string DepartmentName = string.Empty;
    protected int _dept_code = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["dept"] != null)
        {
            _dept_code = int.Parse(Request.QueryString["dept"]);
        }
        var firstOrDefault = _myDmsEntity.Department.FirstOrDefault(d => d.Department_Code == _dept_code);
        if (firstOrDefault != null)
            DepartmentName =
                firstOrDefault.Department_Name;
        if (Author.role_Code == 3)
        {

        }
        if (Author.role_Code == 4)
        {
            pnlManage.Visible = false;
        }
        List<Project> lstProjManaged = _myDmsEntity.Project.Where(p => p.Assigned_To == Author.user_Code).ToList();
        gvProjectsManaged.DataSource = lstProjManaged;
        gvProjectsManaged.DataBind();

        //ddlUsers.DataSource = from u in _myDmsEntity.User
        //                      where u.Department_Code == Author.department_Code
        //                      select new { FullName = u.FirstName + u.LastName, u.User_Code };

        //ddlUsers.DataTextField = "FullName";
        //ddlUsers.DataValueField = "User_Code";
        //ddlUsers.DataBind();
    }

    protected void btnAddTask_Click(object sender, EventArgs e)
    {
        Project ptask = new Project();
        ptask.Project_Title = txtTaskTitle.Text;
        ptask.Project_Desc = txtTastDesc.Text;
        ptask.Assigned_To = Author.user_Code;
        ptask.Creation_By = Author.user_Code;
        ptask.Creation_Date = DateTime.Now;
        ptask.Office_Code = Author.office_Code;
        ptask.Department_Code = _dept_code;
        ptask.Is_Anonymous = cbIsAnony.Checked;

        _myDmsEntity.Project.AddObject(ptask);

        if (_myDmsEntity.SaveChanges() > 0)
        {
            Response.Redirect("javascript:$('#dialogAddChildren').dialog('close');javascript:alert('添加成功');location.reload(true);");
        }
        else
        {
            Response.Redirect("javascript:$('#dialogAddChildren').dialog('close');javascript:alert('添加失败');location.reload(true);");
        }
    }

    protected void gvProjectsManaged_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "OpenChildren")
        {
            int project_Code = int.Parse(e.CommandArgument.ToString());
            var datasource = from task in _myDmsEntity.Task.Where(t => t.Project_Code == project_Code)
                             from user in _myDmsEntity.User
                             where task.Assigned_To == user.User_Code
                             select new
                                        {
                                            task.Task_Desc,
                                            task.Task_Title,
                                            task.Updation_Date,
                                            UserName = user.FirstName + user.LastName
                                        };
            gvManageChildren.DataSource = datasource;
            gvManageChildren.DataBind();
            UpdatePanel2.Update();

            ClientScript.RegisterStartupScript(this.GetType(), null, "<script>$('#dialogManageChildren').dialog('open');</script>");
            //Response.Redirect("javascript:$('#dialogManageChildren').dialog('open');");
            //ClientScript.RegisterClientScriptBlock(this.GetType(), null, "<script>$('#dialogManageChildren').dialog('open');</script>");
        }
    }
}

